﻿namespace AntdUI_HamburgerMenuTabs
{
    /// <summary>
    /// AntdUI 扩展类
    /// 作者：紫剑一品箫
    /// 日期：2025-07-31
    /// </summary>
    public static class AntdUIExtentions
    {
        /// <summary>
        /// 默认品牌色
        /// </summary>
        public static Color DefaultPrimary { get; } = Color.FromArgb(148, 119, 255);

        private readonly static Color darkBgBase = Color.FromArgb(45, 50, 59);
        private readonly static Color darkBgLayout = Color.FromArgb(45, 50, 59);
        private readonly static Color darkDefaultBg = Color.FromArgb(40, 44, 52);
        private readonly static Color darkFore = Color.FromArgb(239, 239, 239);
        private readonly static Color darkTagDefaultBg = Color.FromArgb(32, 36, 43);
        private readonly static Color darkBgElevated = Color.FromArgb(32, 36, 43);
        private readonly static Color lightBgBase = Color.FromArgb(255, 255, 255);
        private readonly static Color lightBgLayout = Color.FromArgb(255, 255, 255);
        private readonly static Color lightDefaultBg = Color.FromArgb(255, 255, 255);
        private readonly static Color lightFore = Color.FromArgb(0, 0, 0);
        private readonly static Color lightTagDefaultBg = Color.FromArgb(255, 255, 255);
        private readonly static Color lightBgElevated = Color.FromArgb(255, 255, 255);

        /// <summary>
        /// 初始化主题配置
        /// </summary>
        /// <param name="window">受影响的 AntdUI.Window 窗体</param>
        /// <param name="header">设置 AntdUI.PageHeader 背景色</param>
        /// <param name="btnDark">设置 AntdUI.Button 按钮的 Toggle</param>
        public static void InitTheme(this AntdUI.Window window, AntdUI.PageHeader? header = null, AntdUI.Button? btnDark = null)
        {
            var config = window.Theme();
            config.Light(lightDefaultBg, lightFore);
            config.Dark(darkDefaultBg, darkFore);
            if (header != null)
            {
                config.Header(header, lightDefaultBg, darkDefaultBg);
            }
            if (btnDark != null) config.Button(btnDark);
        }

        private static void SetColour()
        {
            if (AntdUI.Config.IsDark)
            {
                AntdUI.Style.Set(AntdUI.Colour.BgBase, darkBgBase);
                AntdUI.Style.Set(AntdUI.Colour.BgLayout, darkBgLayout);
                AntdUI.Style.Set(AntdUI.Colour.DefaultBg, darkDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.BgContainer, darkDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.TagDefaultBg, darkTagDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.BgElevated, darkBgElevated);
            }
            else
            {
                AntdUI.Style.Set(AntdUI.Colour.BgBase, lightBgBase);
                AntdUI.Style.Set(AntdUI.Colour.BgLayout, lightBgLayout);
                AntdUI.Style.Set(AntdUI.Colour.DefaultBg, lightDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.BgContainer, lightDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.TagDefaultBg, lightTagDefaultBg);
                AntdUI.Style.Set(AntdUI.Colour.BgElevated, lightBgElevated);
            }
        }

        /// <summary>
        /// 加载主题设置
        /// </summary>
        /// <param name="picker"></param>
        public static void LoadTheme(AntdUI.ColorPicker? picker = null)
        {
            AntdUI.Config.IsDark = Properties.Settings.Default.ThemeDark;
            AntdUI.Style.SetPrimary(Properties.Settings.Default.ThemeColor);
            if (picker != null)
            {
                picker.Value = Properties.Settings.Default.ThemeColor;
            }
            SetColour();
        }

        /// <summary>
        /// 保存主题设置
        /// </summary>
        /// <param name="dark"></param>
        /// <param name="color"></param>
        public static void SaveTheme(bool dark, Color color)
        {
            Properties.Settings.Default.ThemeDark = dark;
            Properties.Settings.Default.ThemeColor = color;
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// 设置并保存主题深色模式
        /// </summary>
        /// <param name="dark"></param>
        public static void SetAndSaveThemeDark(this AntdUI.Window window, bool dark)
        {
            AntdUI.Config.IsDark = dark;
            Properties.Settings.Default.ThemeDark = dark;
            Properties.Settings.Default.Save();

            // 如果自定义了品牌色，那么在这里需要重新设置一下，否则色卡不会更新
            // 注：子窗体可以同步刷新
            AntdUI.Style.SetPrimary(Properties.Settings.Default.ThemeColor);
            SetColour();
            window.Refresh();
        }

        /// <summary>
        /// 设置并保存主题品牌色
        /// 注：子窗体无法同步刷新
        /// </summary>
        /// <param name="window"></param>
        /// <param name="color"></param>
        public static void SetAndSaveThemeColor(this AntdUI.Window window, Color color)
        {
            AntdUI.Style.SetPrimary(color);
            window.Refresh();
            Properties.Settings.Default.ThemeColor = color;
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// 消息提示窗
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg"></param>
        /// <param name="maskClosable">点击蒙层是否允许关闭</param>
        /// <returns></returns>
        public static DialogResult AntModalShowInfo(this Form parent, string msg, bool maskClosable = true)
        {
            Form frm = parent;
            DialogResult res = AntdUI.Modal.open(new AntdUI.Modal.Config(frm, "提示", msg, AntdUI.TType.Info)
            {
                Font = frm.Font,
                MaskClosable = maskClosable,
                CancelText = null,
            });
            return res;
        }

        /// <summary>
        /// 成功提示窗
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg"></param>
        /// <param name="maskClosable">点击蒙层是否允许关闭</param>
        /// <returns></returns>
        public static DialogResult AntModalShowSuccess(this Form parent, string msg, bool maskClosable = true)
        {
            Form frm = parent;
            DialogResult res = AntdUI.Modal.open(new AntdUI.Modal.Config(frm, "成功", msg, AntdUI.TType.Success)
            {
                Font = frm.Font,
                MaskClosable = maskClosable,
                CancelText = null,
            });
            return res;
        }

        /// <summary>
        /// 警告提示窗
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg"></param>
        /// <param name="maskClosable">点击蒙层是否允许关闭</param>
        /// <returns></returns>
        public static DialogResult AntModalShowWarn(this Form parent, string msg, bool maskClosable = false)
        {
            Form frm = parent;
            DialogResult res = AntdUI.Modal.open(new AntdUI.Modal.Config(frm, "警告", msg, AntdUI.TType.Warn)
            {
                Font = frm.Font,
                MaskClosable = maskClosable,
                CancelText = null,
            });
            return res;
        }

        /// <summary>
        /// 错误提示窗
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg"></param>
        /// <param name="maskClosable">点击蒙层是否允许关闭</param>
        /// <returns></returns>
        public static DialogResult AntModalShowError(this Form parent, string msg, bool maskClosable = false)
        {
            Form frm = parent;
            DialogResult res = AntdUI.Modal.open(new AntdUI.Modal.Config(frm, "错误", msg, AntdUI.TType.Error)
            {
                Font = frm.Font,
                MaskClosable = maskClosable,
                CancelText = null,
            });
            return res;
        }

        /// <summary>
        /// 选择提示窗
        /// </summary>
        /// <param name="parent">父窗体</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static DialogResult AntModalShowAsk(this Form parent, string msg)
        {
            Form frm = parent;
            DialogResult res = AntdUI.Modal.open(new AntdUI.Modal.Config(frm, "提示", msg, AntdUI.TType.Warn)
            {
                Font = frm.Font,
                MaskClosable = false,
            });
            return res;
        }
    }
}